package com.westa.smallshop.common;

import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * Created by Dune on 16/1/23.
 */
public abstract class RecyclerViewBaseAdapter<VH extends RecyclerView.ViewHolder>
        extends RecyclerView.Adapter<VH> {
    private OnItemClickListener mOnItemClickListener;
    private OnItemLongClickListener mOnItemLongClickListener;

    @Override
    public void onBindViewHolder(VH holder, int position) {
        initItemClickListener(holder);
    }

    private final void initItemClickListener(final RecyclerView.ViewHolder holder) {

        if (mOnItemClickListener != null && holder.getAdapterPosition() >= 0) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (holder.getAdapterPosition() >= 0) {
                        mOnItemClickListener.onItemClick(holder.itemView, holder.getAdapterPosition());
                    }
                }
            });

        }
        if (mOnItemLongClickListener != null && holder.getAdapterPosition() >= 0) {

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if (holder.getAdapterPosition() >= 0) {
                        return mOnItemLongClickListener.onItemLongClick(holder.itemView, holder.getAdapterPosition());
                    }
                    return  false;
                }
            });
        }
    }

    public void setOnItemLongClickListener(OnItemLongClickListener mOnItemLongClickListener) {
        this.mOnItemLongClickListener = mOnItemLongClickListener;
    }

    public OnItemClickListener getOnItemClickListener() {
        return mOnItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public interface OnItemClickListener {

        void onItemClick(View view, int position);

    }

    public interface OnItemLongClickListener {


        boolean onItemLongClick(View view, int position);
    }
}
